package com.sdj.quick.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.sdj.quick.web.dto.QuickConfig;
import com.sdj.quick.web.util.Coder;
import com.sdj.quick.web.util.HttpClientUtil;
import com.sdj.quick.web.util.SignatureTools;
import com.sdj.quick.web.util.UUIDGenerator;
import com.sdj.quick.web.util.VerifyParamtersUtils;

@Controller
public abstract class BaseController {

	protected transient final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	protected QuickConfig quickConfig;   //快捷配置对象，包含请求路径、证书路径、密码等
	
	/**
	 * 生成订单号
	 * @param bizType
	 * @return
	 */
	protected String getOrderNo(String bizType) {
		return bizType + formatDate(new Date(), "yyyyMMddHHmmssSSS") + UUIDGenerator.getRandomNumber();
	}
	
	
	private String formatDate(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}
	
	/***
	 * 快捷业务测试
	 * @param path
	 * @param merKey
	 * @param t
	 * @return
	 */
	protected <T> Map<String,String> getPostResult(String path,String merKey,T t){
		 Map<String,String> result = new HashMap<String,String>(); 
	      /**请求返回结果**/
	     String resultCode = null;
	     try {
	    	 /**验证打了@Signature注解属性为空参数**/
	    	 VerifyParamtersUtils.verifyParameters(t);
	    	 /**获取需要验签加密数据集合**/
			 Map<String, Object> encryptionMap = VerifyParamtersUtils.toEncryptionMap(t);
		     /**未加密post请求参数集合**/	
		     Map<String, String> paramsMap = VerifyParamtersUtils.toUnEncryptionMap(t);
		     /**拼装验签加密数据**/
		     String postEncryptionParams = VerifyParamtersUtils.mapToPostUrl(encryptionMap)+"&merKey="+merKey;
		     /**sha256摘要加密**/
			 String sha256Sign = Coder.encryptSHA256(postEncryptionParams);
			 /**证书加密数据**/
			 String signature = SignatureTools.encrypt(sha256Sign, quickConfig.getCerPath());
			 /**证书签名数据**/  
	    	 String sign = SignatureTools.sign(signature, quickConfig.getJksPath(), quickConfig.getPassword());
			 paramsMap.put("signature", signature);
			 paramsMap.put("sign", sign);
		     resultCode = HttpClientUtil.post(path, paramsMap);
		     if(StringUtils.isNotEmpty(resultCode)){
		    	 if(resultCode.equals("request_fail")){
		    		 resultCode = "请求失败";
		    	 }
		     }
		     result.put("result",resultCode);
		     logger.info("【快捷测试-post请求链接地址】path = {}",path);
		     logger.info("【快捷测试-post请求链接参数】 params = {}",postEncryptionParams);
		     logger.info("【快捷测试-请求报文参数数据】json = {}",JSON.toJSON(paramsMap));
		     logger.info("【快捷测试-加密数据】signature = {}",signature);
			 logger.info("【快捷测试-验签数据】sign = {}",sign);
		     logger.info("【快捷测试-post请求响应结果】resultCode = {}",resultCode);
		}catch(Exception e){
			resultCode = "请求失败";
			result.put("result",resultCode);
			logger.error("【快捷测试-异常】异常原因:{},异常地址:{}",e,path);
		}
	    return result;
	}
	
}
